package com.xly.webclick.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.xly.webclick.BuildConfig;
import com.xly.webclick.MyApplication;

/**
 * Created by xuleyuan on 2018/8/19
 */

public class SpUtils {
    /**
     * ctv命名为针对版本的数据
     * atv命名为针对所有版本的数据
     */
    public static final String CTV_ON_INIT_OPEN = BuildConfig.VERSION_CODE + "ctv_on_init_open"; //初次打开时间
    public static final String CTV_HAS_OPEN = BuildConfig.VERSION_CODE + "ctv_has_open";//是否已经打开过应用 1第一次打开 2第二次打开
    public static final String CTV_LAST_RATE_OPERATION_TIME = BuildConfig.VERSION_CODE + "ctv_on_init_open"; //最后一次操作评价的时间
    public static final String CTV_USER_RATE_TYPE = BuildConfig.VERSION_CODE + "ctv_user_rate_type";//A B C D四类对应 1,2,3,4  0为首次邀请的新用户
    public static final String FILE_DEFAULT = "";
    public static final String ATV_USERNAME = "username";
    public static final String ATV_PASSWORD = "password";
    public static final String ATV_PHONE = "phone";//经度
    public static final String ATV_IP = "ip";//经度
    public static final String ATV_ADDRESS = "address";//经度
    public static final String ATV_TOKEN = "token";//纬度


    public static int getInt(String key) {
        SharedPreferences sharedPreferences = getSharePreferences(FILE_DEFAULT);
        return sharedPreferences.getInt(key, -1);
    }

    public static boolean getBoolean(String key) {
        SharedPreferences sharedPreferences = getSharePreferences(FILE_DEFAULT);
        return sharedPreferences.getBoolean(key, false);
    }

    public static float getFloat(String key) {
        SharedPreferences sharedPreferences = getSharePreferences(FILE_DEFAULT);
        return sharedPreferences.getFloat(key, -1);
    }

    public static boolean getBoolean(String key, boolean defValue) {
        SharedPreferences sharedPreferences = getSharePreferences(FILE_DEFAULT);
        return sharedPreferences.getBoolean(key, defValue);
    }

    public static String getString(String key) {
        SharedPreferences sharedPreferences = getSharePreferences(FILE_DEFAULT);
        return sharedPreferences.getString(key, FILE_DEFAULT);
    }

    public static String getStringWithDefault(String key, String defStr) {
        SharedPreferences sharedPreferences = getSharePreferences(FILE_DEFAULT);
        return sharedPreferences.getString(key, defStr);
    }

    public static void putInt(String key, int value) {
        SharedPreferences.Editor editor = getEditor(getSharePreferences(FILE_DEFAULT));
        editor.putInt(key, value);
        editor.commit();
    }

    public static void putBoolean(String key, boolean value) {
        SharedPreferences.Editor editor = getEditor(getSharePreferences(FILE_DEFAULT));
        editor.putBoolean(key, value);
        editor.commit();
    }

    public static void putString(String key, String value) {
        SharedPreferences.Editor editor = getEditor(getSharePreferences(FILE_DEFAULT));
        editor.putString(key, value);
        editor.commit();
    }

    public static void putFloat(String key, float value) {
        SharedPreferences.Editor editor = getEditor(getSharePreferences(FILE_DEFAULT));

        editor.putFloat(key, value);
        editor.commit();
    }

    public static void putLong(String key, long value) {
        SharedPreferences.Editor editor = getEditor(getSharePreferences(FILE_DEFAULT));

        editor.putLong(key, value);
        editor.commit();
    }

    public static <T> void putObject(String key, T o) {
        Gson gson = new Gson();
        SharedPreferences.Editor editor = getEditor(getSharePreferences(FILE_DEFAULT));
        String json = gson.toJson(o);
        editor.putString(key, json);
        editor.commit();
    }

    public static <T> T getObject(String key, Class<T> o) {
        SharedPreferences sharedPreferences = getSharePreferences(FILE_DEFAULT);
        Gson gson = new Gson();
        String json = sharedPreferences.getString(key, FILE_DEFAULT);
        if (TextUtils.isEmpty(json)) {
            return null;
//            json = Constant.json;
        }
        return gson.fromJson(json, o);
    }

    public static long getLong(String key) {
        SharedPreferences sharedPreferences = getSharePreferences(FILE_DEFAULT);
        return sharedPreferences.getLong(key, -1);
    }

    public static int getInt(String key, int defValue) {
        SharedPreferences sharedPreferences = getSharePreferences(FILE_DEFAULT);
        return sharedPreferences.getInt(key, defValue);
    }

    public static int getInt(String key, String fileName) {

        SharedPreferences sharedPreferences = getSharePreferences(getFileName(fileName));
        return sharedPreferences.getInt(key, -1);
    }

    public static boolean getBoolean(String key, String fileName) {
        SharedPreferences sharedPreferences = getSharePreferences(getFileName(fileName));
        return sharedPreferences.getBoolean(key, false);
    }

    public static String getString(String key, String fileName) {
        SharedPreferences sharedPreferences = getSharePreferences(getFileName(fileName));
        return sharedPreferences.getString(key, null);
    }

    public static float getFloat(String key, String fileName) {
        SharedPreferences sharedPreferences = getSharePreferences(getFileName(fileName));
        return sharedPreferences.getFloat(key, -1);
    }

    public static long getLong(String key, String fileName) {
        SharedPreferences sharedPreferences = getSharePreferences(getFileName(fileName));
        return sharedPreferences.getLong(key, -1);
    }

    public static void putInt(String key, int value, String fileName) {
        SharedPreferences.Editor editor = getEditor(getSharePreferences(getFileName(fileName)));
        editor.putInt(key, value);
        editor.commit();

    }

    public static void putBoolean(String key, boolean value, String fileName) {
        SharedPreferences.Editor editor = getEditor(getSharePreferences(getFileName(fileName)));

        editor.putBoolean(key, value);
        editor.commit();
    }

    public static void putString(String key, String value, String fileName) {
        SharedPreferences.Editor editor = getEditor(getSharePreferences(getFileName(fileName)));

        editor.putString(key, value);
        editor.commit();
    }

    public static void putFloat(String key, float value, String fileName) {
        SharedPreferences.Editor editor = getEditor(getSharePreferences(getFileName(fileName)));

        editor.putFloat(key, value);
        editor.commit();
    }

    public static void putLong(String key, long value, String fileName) {
        SharedPreferences.Editor editor = getEditor(getSharePreferences(getFileName(fileName)));

        editor.putLong(key, value);
        editor.commit();
    }

    private static SharedPreferences getSharePreferences(String fileName) {
        return MyApplication.getInstance().getSharedPreferences(fileName, Context.MODE_PRIVATE);

    }

    private static SharedPreferences.Editor getEditor(SharedPreferences sharedPreferences) {
        return sharedPreferences.edit();
    }

    public static String getFileName(String fileName) {
        if (!TextUtils.isEmpty(fileName)) {
            return fileName;
        } else {
            return FILE_DEFAULT;
        }
    }


    /**
     * 清空新版本需要清空的数据
     *
     * @param context context
     * @return result
     */
    public static boolean clearVersionPreference(Context context) {

        return false;
    }

    /**
     * 清空所有数据
     *
     * @param context context
     * @return result
     */
    public static boolean clearAllPreference(Context context) {
        return false;
    }
}
